package LevelEditor;

import ClientGraphics.GraphicsMain;
import Objects.Fan;
import Objects.GameObject;
import GameEngine.GameConstants;
import GameEngine.GameConstants.Levels;
import GameEngine.GameConstants.Modifier;
import GameEngine.GameConstants.Thing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 * Top level panel.
 * @author jh20
 *
 */

public class MainPanel extends JPanel {
	
	private LevelViewer levelView;
    private GraphicsMain gMain;
	private JComponent levelDraw; // where the preview of the level is drawn
	private ToolPanel toolP;
    private FunctionPanel topP;
	
	private int LEVEL_WIDTH = 1000;
	private int LEVEL_HEIGHT = 800;

    private int numPlayers = 1; // number of players intended for this level
	
	private GameConstants.Thing currentType = Thing.BIGFAN;
	private Modifier dir = Modifier.UPLEFT;
	
	public MainPanel() {
		super();

        Dimension levelDimension = new Dimension(LEVEL_WIDTH, LEVEL_HEIGHT);
        gMain = new GraphicsMain(Levels.DEFAULT, levelDimension, LEVEL_WIDTH, LEVEL_HEIGHT);
        levelDraw = gMain.getGraphicsPanel();
		
		levelDraw.setPreferredSize(new Dimension(LEVEL_WIDTH, LEVEL_HEIGHT));
		
		levelView = new LevelViewer(this, levelDraw, gMain);
		
		this.setLayout(new BorderLayout());
		this.add(levelView, BorderLayout.CENTER);

        JPanel sideP = new JPanel();
        sideP.setLayout(new BorderLayout());
		
		toolP = new ToolPanel(this);
		sideP.add(toolP, BorderLayout.CENTER);

        topP = new FunctionPanel(this);
        sideP.add(topP, BorderLayout.NORTH);

        this.add(sideP, BorderLayout.WEST);
	}

    /**
     * Delegates levelView to resetting the level.
     */

    public void resetLevel() {
        levelView.resetLevel();
    }

    public void resetPreview() {
        levelView.resetPreview();
    }

    public void setTempId(short i) {
        levelView.setTempId(i);
    }
	
	/**
	 * Changes the size of the level
	 * @param x
	 * @param y
	 */
	
	public void setLevelSize(int x, int y) {
        if (x <= 9999 && x >= 0 && y <= 9999 && y >= 0) {
            // if valid parameters
            gMain.resize(new Dimension(x, y));
            gMain.setLevelH(y);
            gMain.setLevelW(x);
            levelDraw.setPreferredSize(new Dimension(x, y));
            levelDraw.revalidate();
            if (toolP != null) {
                toolP.setSizeFields(x, y);
            }
        // handle deletion and whatnot here

            if (levelView != null) {
                levelView.resizeClear(x, y);
            }
            LEVEL_WIDTH = x;
            LEVEL_HEIGHT = y;

        } else {
            if (toolP != null) {
                toolP.setSizeFields(levelDraw.getWidth(), levelDraw.getHeight());
            }
        }
	}

    public int getNumPlayers() {
        return numPlayers;
    }

    public void setNumPlayers(int n) {
        if (n <= 9 && n > 0) {
            // if valid parameters
            numPlayers = n;
            if (toolP != null) {
                toolP.setNumPlayerField(n);
            }
        } else {
            // otherwise, reset the field
            if (toolP != null) {
                toolP.setNumPlayerField(numPlayers);
            }
        }
    }

    /**
     * Delegation: reads and writes to the specified file by passing to levelView
     */

    public void write(String output) {
        levelView.write(output);
    }

    public void read(String input) {
        levelView.read(input);
    }

    /**
     *  delegation to levelView
     * @see LevelViewer.resetFirstClick()
     */

    public void resetFirstClick() {
        levelView.resetFirstClick();
    }
	
	public int getSizeX() {
		return LEVEL_WIDTH;
	}
	
	public int getSizeY() {
		return LEVEL_HEIGHT;
	}
	
	public void setCurrentType(GameConstants.Thing t) {
		currentType = t;
	}
	
	public Thing getCurrentType() {
		return currentType;
	}
	
	public void setDir(Modifier m) {
		dir = m;
	}
	
	public Modifier getDir() {
		return dir;
	}
	
	

}
